package com.zhoug.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

import java.io.File;

/**
 * @Author HK-LJJ
 * @Date 2020/3/25
 * @Description
 */
public class MyUtils {
    private static final String TAG = ">>>MyUtils";
    /**
     * 保存图片的缓存路径
     * @return
     */
    public static  String getCacheImagePath(Context context) {
        File cacheDir = context.getExternalCacheDir();
        File file = new File(cacheDir, "image/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return file.getAbsolutePath();
    }

    /**
     * 根据rectRatio比例裁剪图片
     *
     * @param rectRatio 比例
     * @param bitmap
     * @return
     */
    public  static Bitmap cutBitmap(RectF rectRatio, Bitmap bitmap) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Rect rect = new Rect((int) (w * rectRatio.left), (int) (h * rectRatio.top), (int) (w * rectRatio.right), (int) (h * rectRatio.bottom));
        Log.d(TAG, "cutBitmap:rectRatio=" + rectRatio);
        Log.d(TAG, "width:" + w + ",height=" + h);
        Log.d(TAG, "cutBitmap:rect=" + rect);
        int x = rect.left;
        int y = rect.top;
        int width = rect.right - rect.left;
        int height = rect.bottom - rect.top;
        return Bitmap.createBitmap(bitmap, x, y, width, height);
    }


}
